<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="炉号" prop="batch">
        <el-input
          v-model="queryParams.batch"
          placeholder="请输入炉号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入产品型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="浇筑数量" prop="num">-->
<!--        <el-input-->
<!--          v-model="queryParams.num"-->
<!--          placeholder="请输入浇筑数量"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="废品数" prop="wasteNum">-->
<!--        <el-input-->
<!--          v-model="queryParams.wasteNum"-->
<!--          placeholder="请输入废品数"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="合格率" prop="fpy">-->
<!--        <el-input-->
<!--          v-model="queryParams.fpy"-->
<!--          placeholder="请输入合格率"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="工序时间">
        <el-date-picker
          v-model="daterangeWorkTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['work:basic:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['work:basic:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['work:basic:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['work:basic:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="basicList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="炉号" align="center" prop="batch" />
      <el-table-column label="铸件标识" align="center" prop="sign" />
      <el-table-column label="产品型号" align="center" prop="model" />
      <el-table-column label="浇筑数量" align="center" prop="num" />
      <el-table-column label="废品数" align="center" prop="wasteNum" />
      <el-table-column label="入库数" align="center" prop="successNum" />
      <el-table-column label="合格率" align="center" prop="fpy" />
      <el-table-column label="产品类型" align="center" prop="typeName" />
      <el-table-column
        label="工序时间"
        align="center"
        prop="workTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.workTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-row>
            <el-col :span="12">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleProcess(scope.row)"
            v-hasPermi="['work:basic:edit']"
            >工序</el-button
          >
            </el-col>

            <el-col :span="12">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['work:basic:edit']"
            >修改</el-button
          >
            </el-col>
          </el-row>


          <el-row>
            <el-col :span="12">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleCheck(scope.row)"
            v-hasPermi="['work:basic:edit']"
            >检验</el-button
          >
            </el-col>

            <el-col :span="12">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['work:basic:remove']"
            >删除</el-button
          >
            </el-col>

          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流转作业卡对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item
          label="炉号"
          prop="batch"
          v-show="title == '添加流转作业卡'"
        >
          <el-input v-model="form.batch" placeholder="请输入炉号" />
        </el-form-item>
        <el-form-item label="铸件标识" prop="sign">
          <el-input v-model="form.sign" placeholder="请输入铸件标识" />
        </el-form-item>
        <el-form-item label="产品型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入产品型号" />
        </el-form-item>
        <el-form-item label="浇筑数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入浇筑数量" />
        </el-form-item>
        
        <el-form-item label="产品类型" prop="type">
          <el-select v-model="form.type">
            <el-option label="球铁产品" value="1" />
            <el-option label="灰铁产品" value="2" />
            <el-option label="摩配产品" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="工序时间" prop="workTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.workTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择工序时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            :rows="3"
            v-model="form.remark"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="工序卡"
      :visible.sync="processVisible"
      width="1300px"
      append-to-body
    >
      <process :batchId="batchId" v-if="processVisible" @closeMyself="hiddenProcess($event)" />
    </el-dialog>

    <el-dialog
      title="检验"
      :visible.sync="checkVisible"
      width="800px"
      append-to-body
    >
      <check :batchId="batchId" v-if="checkVisible" @closeMyself="hiddenCheck($event)"/>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBasic,
  getBasic,
  delBasic,
  addBasic,
  updateBasic,
  exportBasic,
} from "@/api/work/basic";
import Process from "../process/index";
import Check from "../check/index";
import *as product from "@/common/productEnum";
export default {
  name: "Basic",
  components: {
    Process,
    Check,
  },
  data() {
    return {
      product:product.PRODUCT,
      // 遮罩层
      loading: true,
      // 选中数组
      batches: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流转作业卡表格数据
      basicList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 工序时间时间范围
      daterangeWorkTime: [],
      //工序添加卡显示
      processVisible: false,
      //检验页面显示
      checkVisible:false,
      //批次号
      batchId:null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        batch: null,
        model: null,
        num: null,
        wasteNum: null,
        fpy: null,
        workTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        batch: [{ required: true, message: "炉号不能为空", trigger: "blur" }],
        model: [
          { required: true, message: "产品型号不能为空", trigger: "blur" },
        ],
        num: [
          { required: true, message: "浇筑数量不能为空", trigger: "blur" },
          {
            pattern: /^\d+$/,
            message: "浇筑数量只能为数字",
            trigger: ["blur", "change"],
          },
        ],
        workTime: [
          { required: true, message: "工序时间不能为空", trigger: "blur" },
        ],
        wasteNum: [
          {
            pattern: /^\d+$/,
            message: "废品数只能为数字",
            trigger: ["blur", "change"],
          },
        ],
        type: [
           { required: true, message: "产品类型不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询流转作业卡列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeWorkTime && "" != this.daterangeWorkTime) {
        this.queryParams.params["beginWorkTime"] = this.daterangeWorkTime[0];
        this.queryParams.params["endWorkTime"] = this.daterangeWorkTime[1];
      }
      listBasic(this.queryParams).then((response) => {
        this.basicList = response.rows;
        this.basicList.forEach(basic=>{
          basic.typeName = this.product[basic.type];
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.processVisible = false;
      this.checkVisible = false;
      this.title = "";
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        // id: null,
        batch: null,
        sign: null,
        model: null,
        num: null,
        wasteNum: null,
        fpy: null,
        type: null,
        workTime: null,
        createTime: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeWorkTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.batches = selection.map((item) => item.batch);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加流转作业卡";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const batch = row.batch || this.batches;
      getBasic(batch).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改流转作业卡";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.title === "修改流转作业卡") {
            updateBasic(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBasic(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const batches = row.batch || this.batches;
      this.$confirm(
        '是否确认删除流转作业卡编号为"' + batches + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delBasic(batches);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有流转作业卡数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportBasic(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    handleProcess(row) {
      this.batchId = row.batch;
      this.processVisible = true;
    },
    handleCheck(row) {
      this.batchId = row.batch;
      this.checkVisible = true;
    },
    hiddenProcess(even){
      this.processVisible = even;
    },
    hiddenCheck(even){
      this.checkVisible = even;
    }
  },
};
</script>
